package com.easybuilder.blog.controller;

import com.easybuilder.blog.dto.ArticleDTO;
import com.easybuilder.blog.entity.Article;
import com.easybuilder.blog.entity.ArticleCriteria;
import com.easybuilder.blog.service.ArticleService;
import com.easybuilder.common.base.controller.BaseController;
import com.easybuilder.common.base.entity.Page;
import com.easybuilder.common.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController extends BaseController {

    @Autowired
    private ArticleService articleService;


    /**
     * @Author zhangxingshuai
     * @Description //TODO 根据文章id，获取文章详细信息
     * @Date 17:57 2022/1/5
     * @Param [id]
     * @return java.lang.Object
     **/
    @GetMapping("/{id}")
    public Object getArticleById(@PathVariable(name = "id", required = true) Long id) {
        ArticleDTO articleDTO = articleService.getArticleById(id);
        return OK(articleDTO);
    }

    @PostMapping("/page")
    public Object page(HttpServletRequest request, HttpServletResponse response, @RequestBody ArticleCriteria articleCriteria){
        int pageNum = StringUtil.isEmpty(request.getHeader("page_num")) ? 0 : Integer.parseInt(request.getHeader("page_num"));
        int pageSize = StringUtil.isEmpty(request.getHeader("page_size")) ? 10 : Integer.parseInt(request.getHeader("page_size"));
        // 获取数据总量
        int total = articleService.getTotal(articleCriteria);
        response.setHeader("total", total+"");
        // 获取分页数据
        Page<ArticleDTO> page = articleService.page(pageNum, pageSize, articleCriteria);

        return OK(page);
    }

    /***
     * @Author zhangxingshuai
     * @Description //TODO 发布文章
     * @Date 13:56 2021/12/31
     * @Param []
     * @return java.lang.Object
     **/
    @PostMapping("/publish")
    public Object publish(@RequestBody Article article) {
        log.info("发布文章，请求参数：{}", article);
        if (article == null) {
            return Error(HttpStatus.BAD_REQUEST, "文章标题或内容不能为空");
        }
        if (article != null) {
            if (StringUtil.isEmpty(article.getTitle())) {
                return Error(HttpStatus.BAD_REQUEST, "文章标题不能为空！");
            }
            if (StringUtil.isEmpty(article.getContent())) {
                return Error(HttpStatus.BAD_REQUEST, "文章内容不能为空！");
            }
        }
        article = articleService.publish(article);
        return OK(article);
    }


    /** 批量删除 */
    @PostMapping("/batch_delete")
    public Object batchDelete(@RequestBody List<Long> ids) {
        if (ids == null || ids.size() == 0) {
            return Error(HttpStatus.BAD_REQUEST, "删除数据id不能为空");
        }
        int delete = articleService.batchDelete(ids);
        return OK("已删除"+delete+"条数据");
    }


}
